{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['2015q1', '2015q2', '2015q3', '2015q4', '2016q1', '2016q2', '2016q3', '2016q4', '2017q1', '2017q2', '2017q3', '2017q4', '2018q1', '2018q2', '2018q3', '2018q4', '2019q1', '2019q2', '2019q3', '2019q4', '2020q1', '2020q2']\n"
     ]
    }
   ],
   "source": [
    "seasons = []\n",
    "for i in range(2015, 2020):\n",
    "    for j in range(1, 5):\n",
    "        seasons.append(str(i) + 'q' + str(j))\n",
    "seasons.append('2020q1')\n",
    "seasons.append('2020q2')\n",
    "print(seasons)\n",
    "        \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 每个季度作为一个dataframe,以字典方式存入\n",
    "# 市值数据，资产负债数据， 现金流数据，利润数据，财务指标数据\n",
    "dict_valuation = {}\n",
    "dict_balance = {}\n",
    "dict_cash_flow = {}\n",
    "dict_income = {}\n",
    "dict_indicator = {}\n",
    "for season in seasons:   \n",
    "    # 市值数据\n",
    "    dict_valuation[season] = get_fundamentals(query(valuation.id, valuation.code, valuation.capitalization, valuation.market_cap, valuation.circulating_cap), statDate  = season)\n",
    "    dict_valuation[season]['pubSeason'] = season\n",
    "    \n",
    "    # 资产负债数据\n",
    "    dict_balance[season] = get_fundamentals(query(balance.id, balance.code, balance.cash_equivalents, balance.bill_receivable), statDate  = season)\n",
    "    dict_balance[season]['pubSeason'] = season\n",
    "    \n",
    "    # 现金流数据\n",
    "    dict_cash_flow[season] = get_fundamentals(query(cash_flow.id, cash_flow.code, cash_flow.goods_sale_and_service_render_cash), statDate  = season)\n",
    "    dict_cash_flow[season]['pubSeason'] = season\n",
    "    \n",
    "    # 利润数据\n",
    "    dict_income[season] = get_fundamentals(query(income.id, income.code, income.sale_expense, income.administration_expense, income.financial_expense), statDate  = season)\n",
    "    dict_income[season]['pubSeason'] = season\n",
    "    \n",
    "    # 财务指标数据\n",
    "    dict_indicator[season] = get_fundamentals(query(indicator.id, indicator.code, indicator.inc_total_revenue_annual), statDate  = season)\n",
    "    dict_indicator[season]['pubSeason'] = season\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(72552, 6)\n",
      "(72884, 5)\n",
      "(73302, 4)\n",
      "(73348, 6)\n",
      "(72882, 4)\n"
     ]
    }
   ],
   "source": [
    "# 保存到csv\n",
    "save_files = ['市值数据', '资产负债数据', '现金流数据', '利润数据', '财务数据指标']\n",
    "\n",
    "\n",
    "for i, df in enumerate([dict_valuation, dict_balance, dict_cash_flow, dict_income, dict_indicator]):    \n",
    "    df1 = pd.concat([df[s] for s in seasons], axis = 0)\n",
    "    print(df1.shape)\n",
    "    df1.to_csv(save_files[i] + '.csv', index = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "MarkDown菜单",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
